﻿#include "All.h"
#include <graphics.h>


// 全局变量定义
const char PASSWORD_MASK = '*';
bool g_isInputUserID = false;
bool g_isInputPassword = false;
extern int userCount;
std::wstring g_userId;
std::wstring g_password;
bool g_showCursor = false;
clock_t g_cursorTimer = 0;
int g_cursorPos = 0;
std::wstring g_username = L"";

//注册相关变量
std::wstring g_userName;
std::wstring g_confirmPassword;
std::wstring g_question;
std::wstring g_answer;
bool g_isInputUserName = false;
bool g_isInputConfirmPassword = false;
bool g_isInputQuestion = false;
bool g_isInputAnswer = false;


std::wstring g_newPassword;
bool g_isInputNewPassword = false;


struct Button {
    int x, y, w, h;
    COLORREF colorNormal;
    COLORREF colorHover;
    COLORREF colorCurrent;
    bool isHover;
};



IMAGE g_bgImg1;


Button g_btnUserID1 = { 138, 198, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnPassword1 = { 138, 248, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnLogin = { 128, 308, 60, 32, WHITE, RGB(240, 240, 210), WHITE, false };
Button g_btnRegister1 = { 218, 308, 60, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnForget = { 170, 378, 60, 20, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnEsc1 = { 320, 650, 60, 40, WHITE, RGB(240,240,210), WHITE , false };


Button g_btnUserName = { 148, 148, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnUserID2 = { 148, 198, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnPassword2 = { 148, 248, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnConfirmPassword = { 148, 298, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnQuestion = { 148, 348, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnAnswer = { 148, 398, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnRegister2 = { 180, 460, 60, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnEsc2 = { 320, 650, 60, 40, WHITE, RGB(240,240,210), WHITE , false };


Button g_btnUserID3 = { 148, 198, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnNewPassword = { 148, 248, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnQuestion3 = { 148, 298, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnAnswer3 = { 148, 348, 200, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnResetPassword = { 180, 410, 100, 32, WHITE, RGB(240,240,210), WHITE , false };
Button g_btnEsc3 = { 320, 650, 60, 40, WHITE, RGB(240,240,210), WHITE , false };

// 绘制文本框
void drawTextBox(const Button& btn) {
    setfillcolor(btn.colorCurrent);
    solidrectangle(btn.x, btn.y, btn.x + btn.w, btn.y + btn.h);

    COLORREF borderColor = BLACK;
    if (&btn == &g_btnUserID1 || &btn == &g_btnPassword1 ||
        &btn == &g_btnUserID2 || &btn == &g_btnPassword2 ||
        &btn == &g_btnConfirmPassword || &btn == &g_btnQuestion ||
        &btn == &g_btnAnswer || &btn == &g_btnUserID3 ||
        &btn == &g_btnNewPassword || &btn == &g_btnQuestion3 ||
        &btn == &g_btnAnswer3) {
        bool isActive =
            (&btn == &g_btnUserID1 && g_isInputUserID) ||
            (&btn == &g_btnPassword1 && g_isInputPassword) ||
            (&btn == &g_btnUserID2 && g_isInputUserID) ||
            (&btn == &g_btnPassword2 && g_isInputPassword) ||
            (&btn == &g_btnConfirmPassword && g_isInputConfirmPassword) ||
            (&btn == &g_btnQuestion && g_isInputQuestion) ||
            (&btn == &g_btnAnswer && g_isInputAnswer) ||
            (&btn == &g_btnUserID3 && g_isInputUserID) ||
            (&btn == &g_btnNewPassword && g_isInputNewPassword) ||
            (&btn == &g_btnQuestion3 && g_isInputQuestion) ||
            (&btn == &g_btnAnswer3 && g_isInputAnswer);

        borderColor = isActive ? RGB(0, 150, 255) : BLACK;
    }
    else {
        borderColor = btn.colorCurrent;
    }
    setlinecolor(borderColor);
    rectangle(btn.x, btn.y, btn.x + btn.w, btn.y + btn.h);
}

// 更新按钮状态
void updateButtonState(Button& btn, const ExMessage& msg) {
    btn.isHover = (msg.x >= btn.x && msg.x <= btn.x + btn.w &&
        msg.y >= btn.y && msg.y <= btn.y + btn.h);
    btn.colorCurrent = btn.isHover ? btn.colorHover : btn.colorNormal;
}

// 通用输入处理函数
void processInput(wchar_t c, std::wstring& str, int maxLen) {
    if (c == L'\b') { // 退格键
        if (g_cursorPos > 0) {
            str.erase(--g_cursorPos, 1);
        }
    }
    else if (c == L'\r') { // 回车键，这里不处理，由按钮点击处理
        // 不做处理
    }
    else if (str.length() < maxLen) { // 普通字符
        str.insert(g_cursorPos, 1, c);
        g_cursorPos++;
    }
}

// 处理登录输入
void handleLoginInput(UINT message, WPARAM wParam, LPARAM lParam) {
    if (message == WM_CHAR) {
        wchar_t c = (wchar_t)wParam;

        int maxLen = 20; // 最大长度限制
        if (g_isInputUserID) {
            processInput(c, g_userId, maxLen);
        }
        else if (g_isInputPassword) {
            processInput(c, g_password, maxLen);
        }

        g_showCursor = true;
        g_cursorTimer = clock();
    }
}

// 廣過補秘侃尖
void handleRegisterInput(UINT message, WPARAM wParam, LPARAM lParam) {
    if (message == WM_CHAR) {
        wchar_t c = (wchar_t)wParam;

        int maxLen = 20; // 最大长度限制
        if (g_isInputUserName) {
            processInput(c, g_userName, maxLen);
        }
        else if (g_isInputUserID) {
            processInput(c, g_userId, maxLen);
        }
        else if (g_isInputPassword) {
            processInput(c, g_password, maxLen);
        }
        else if (g_isInputConfirmPassword) {
            processInput(c, g_confirmPassword, maxLen);
        }
        else if (g_isInputQuestion) {
            processInput(c, g_question, maxLen);
        }
        else if (g_isInputAnswer) {
            processInput(c, g_answer, maxLen);
        }

        g_showCursor = true;
        g_cursorTimer = clock();
    }
}

// 梨芝畜鷹補秘侃尖
void handleForgetInput(UINT message, WPARAM wParam, LPARAM lParam) {
    if (message == WM_CHAR) {
        wchar_t c = (wchar_t)wParam;

        int maxLen = 20; // 最大长度限制
        if (g_isInputUserID) {
            processInput(c, g_userId, maxLen);
        }
        else if (g_isInputNewPassword) {
            processInput(c, g_newPassword, maxLen);
        }
        else if (g_isInputQuestion) {
            processInput(c, g_question, maxLen);
        }
        else if (g_isInputAnswer) {
            processInput(c, g_answer, maxLen);
        }

        g_showCursor = true;
        g_cursorTimer = clock();
    }
}

// 鞠村順中
void signIn() {
    ExMessage msg;
    extern const COLORREF ACTIVE_BORDER_COLOR;
    extern user users[MAX_USER];
    users[0].loadDataFromFile(L".\\data\\ManManage.txt");

    while (true) {
        while (peekmessage(&msg, -1)) {
            if (msg.message == WM_QUIT) {
                return;
            }

            switch (msg.message) {
            case WM_MOUSEMOVE: {
                updateButtonState(g_btnUserID1, msg);
                updateButtonState(g_btnPassword1, msg);
                updateButtonState(g_btnLogin, msg);
                updateButtonState(g_btnRegister1, msg);
                updateButtonState(g_btnForget, msg);
                updateButtonState(g_btnEsc1, msg);
                break;
            }
            case WM_LBUTTONDOWN: {
                updateButtonState(g_btnUserID1, msg);
                updateButtonState(g_btnPassword1, msg);
                updateButtonState(g_btnLogin, msg);
                updateButtonState(g_btnRegister1, msg);
                updateButtonState(g_btnForget, msg);
                updateButtonState(g_btnEsc1, msg);

                if (g_btnUserID1.isHover) {
                    g_isInputUserID = true;
                    g_isInputPassword = false;
                    g_cursorPos = g_userId.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnPassword1.isHover) {
                    g_isInputPassword = true;
                    g_isInputUserID = false;
                    g_cursorPos = g_password.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else {
                    g_isInputPassword = false;
                    g_isInputUserID = false;
                }

                if (g_btnLogin.isHover) {
                    if (validateUser(g_userId, g_password)) {
                        // 获取并设置用户名
                        for (int i = 0; i < userCount; i++) {
                            if (users[i].id == g_userId) {
                                g_username = users[i].username;
                                break;
                            }
                        }
                        MessageBox(NULL, _T("登录成功"), _T("提示"), MB_OK);
                        Menu();
                    }
                    else {
                        MessageBox(NULL, _T("ID或密码错误"), _T("错误"), MB_OK);
                    }
                }
                if (g_btnEsc1.isHover) {
                    Welcome();
                }
                if (g_btnRegister1.isHover) {
                    // 切换到注册界面
                    Register();
                }
                if (g_btnForget.isHover) {
                    // 切换到忘记密码界面
                    forgetPassword();
                }
                break;
            }
            case WM_KEYDOWN:
                if (msg.vkcode == VK_BACK) {
                    if (g_isInputUserID && g_cursorPos > 0) {
                        g_userId.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputPassword && g_cursorPos > 0) {
                        g_password.erase(--g_cursorPos, 1);
                    }
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_LEFT) {
                    g_cursorPos = max(0, g_cursorPos - 1);
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_RIGHT) {
                    int maxPos = g_isInputUserID ? g_userId.length() : g_password.length();
                    g_cursorPos = min(maxPos, g_cursorPos + 1);
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_UP) {
                    g_isInputUserID = true;
                    g_isInputPassword = false;
                    g_cursorPos = g_userId.length();
                }
                else if (msg.vkcode == VK_DOWN) {
                    g_isInputUserID = false;
                    g_isInputPassword = true;
                    g_cursorPos = g_password.length();
                }
                else if (msg.vkcode == VK_RETURN) {
                    if (g_isInputUserID) {
                        // 从用户ID切换到密码
                        g_isInputUserID = false;
                        g_isInputPassword = true;
                        g_cursorPos = g_password.length();
                    }
                    else if (g_isInputPassword) {
                        if (validateUser(g_userId, g_password)) {
                            MessageBox(NULL, _T("登录成功"), _T("提示"), MB_OK);
                            Menu();
                        }
                        else {
                            MessageBox(NULL, _T("ID或密码错误"), _T("错误"), MB_OK);
                        }
                    }
                    
                }
                else if (msg.vkcode == VK_TAB) {
                    if (g_isInputUserID) {
                        // 从用户ID切换到密码
                        g_isInputUserID = false;
                        g_isInputPassword = true;
                        g_cursorPos = g_password.length();
                    }
                    else if (g_isInputPassword) {
                        // 从密码切换到登录按钮（可选）
                        // 或者切换回用户ID
                        g_isInputPassword = false;
                        g_isInputUserID = true;
                        g_cursorPos = g_userId.length();
                    }
                    else {
                        // 默认切换到用户ID
                        g_isInputUserID = true;
                        g_isInputPassword = false;
                        g_cursorPos = g_userId.length();
                    }
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                break;
            case WM_CHAR:
                handleLoginInput(msg.message, msg.wParam, msg.lParam);
                break;
            }
        }

        // 处理光标闪烁
        if (clock() - g_cursorTimer > 500) {
            g_showCursor = !g_showCursor;
            g_cursorTimer = clock();
        }

        // 绘制界面
        drawMainUI();

        // 添加短暂延迟
        Sleep(10);
    }
}

// 麼順中紙崙
void drawMainUI()
{
    BeginBatchDraw();

    loadimage(&g_bgImg1, _T("png\\g_bgImg1.png"));
    putimage(0, 0, &g_bgImg1);

    drawTextBox(g_btnUserID1);
    drawTextBox(g_btnPassword1);
    drawTextBox(g_btnLogin);
    drawTextBox(g_btnRegister1);
    drawTextBox(g_btnForget);
    drawTextBox(g_btnEsc1);

    settextstyle(24, 0, _T("宋体"));
    setbkmode(TRANSPARENT);
    settextcolor(WHITE);
    outtextxy(50, 200, _T("ID"));
    outtextxy(50, 250, _T("密码"));
    settextcolor(BLACK);
    outtextxy(130, 310, _T("登录"));
    outtextxy(220, 310, _T("注册"));
    settextcolor(RED);
    settextstyle(15, 0, _T("宋体"));
    outtextxy(170, 380, _T("忘记密码"));

    settextcolor(RED);
    settextstyle(24, 0, _T("宋体"));
    outtextxy(322, 652, _T("退出"));

    if (g_userId.empty() && !g_isInputUserID) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnUserID1.x + 5, g_btnUserID1.y + 5, _T("请输入ID"));
    }
    if (g_password.empty() && !g_isInputPassword) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnPassword1.x + 5, g_btnPassword1.y + 5, _T("请输入密码"));
    }

    settextstyle(22, 0, _T("宋体"));

    settextcolor(BLACK);
    outtextxy(g_btnUserID1.x + 5, g_btnUserID1.y + 5, g_userId.c_str());
    std::wstring showPass(g_password.size(), PASSWORD_MASK);
    outtextxy(g_btnPassword1.x + 5, g_btnPassword1.y + 5, showPass.c_str());

    if (clock() - g_cursorTimer > 500) {
        g_showCursor = !g_showCursor;
        g_cursorTimer = clock();
    }

    if (g_showCursor) {
        Button* activeBtn = nullptr;
        if (g_isInputUserID) activeBtn = &g_btnUserID1;
        else if (g_isInputPassword) activeBtn = &g_btnPassword1;

        if (activeBtn != nullptr) {
            std::wstring displayStr;
            int textWidth = 0;

            if (g_isInputUserID) {
                displayStr = g_userId.substr(0, g_cursorPos);
                textWidth = textwidth(displayStr.c_str());
            }
            else if (g_isInputPassword) {
                displayStr = g_password.substr(0, g_cursorPos);
                textWidth = textwidth(displayStr.c_str());
            }

            setlinestyle(PS_SOLID, 2);
            setcolor(RGB(0, 150, 255));
            line(activeBtn->x + 5 + textWidth, activeBtn->y + 5,
                activeBtn->x + 5 + textWidth, activeBtn->y + activeBtn->h - 5);
        }
    }

    EndBatchDraw();
}

// 注册界面
void Register() {
    ExMessage msg;
    extern const COLORREF ACTIVE_BORDER_COLOR;
    extern user users[MAX_USER];
    users[0].loadDataFromFile(L".\\data\\ManManage.txt");

    // 嶷崔補秘
    g_userName.clear();
    g_userId.clear();
    g_password.clear();
    g_confirmPassword.clear();
    g_question.clear();
    g_answer.clear();
    g_isInputUserName = false;
    g_isInputUserID = false;
    g_isInputPassword = false;
    g_isInputConfirmPassword = false;
    g_isInputQuestion = false;
    g_isInputAnswer = false;

    while (true) {
        while (peekmessage(&msg, -1)) {
            if (msg.message == WM_QUIT) {
                return;
            }

            switch (msg.message) {
            case WM_MOUSEMOVE: {
                updateButtonState(g_btnUserName, msg);
                updateButtonState(g_btnUserID2, msg);
                updateButtonState(g_btnPassword2, msg);
                updateButtonState(g_btnConfirmPassword, msg);
                updateButtonState(g_btnQuestion, msg);
                updateButtonState(g_btnAnswer, msg);
                updateButtonState(g_btnRegister2, msg);
                updateButtonState(g_btnEsc2, msg);
                break;
            }
            case WM_LBUTTONDOWN: {
                updateButtonState(g_btnUserName, msg);
                updateButtonState(g_btnUserID2, msg);
                updateButtonState(g_btnPassword2, msg);
                updateButtonState(g_btnConfirmPassword, msg);
                updateButtonState(g_btnQuestion, msg);
                updateButtonState(g_btnAnswer, msg);
                updateButtonState(g_btnRegister2, msg);
                updateButtonState(g_btnEsc2, msg);

                // 侃尖補秘崇泣似
                if (g_btnUserName.isHover) {
                    g_isInputUserName = true;
                    g_isInputUserID = false;
                    g_isInputPassword = false;
                    g_isInputConfirmPassword = false;
                    g_isInputQuestion = false;
                    g_isInputAnswer = false;
                    g_cursorPos = g_userName.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnUserID2.isHover) {
                    g_isInputUserName = false;
                    g_isInputUserID = true;
                    g_isInputPassword = false;
                    g_isInputConfirmPassword = false;
                    g_isInputQuestion = false;
                    g_isInputAnswer = false;
                    g_cursorPos = g_userId.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnPassword2.isHover) {
                    g_isInputPassword = true;
                    g_isInputUserName = false;
                    g_isInputUserID = false;
                    g_isInputConfirmPassword = false;
                    g_isInputQuestion = false;
                    g_isInputAnswer = false;
                    g_cursorPos = g_password.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnConfirmPassword.isHover) {
                    g_isInputConfirmPassword = true;
                    g_isInputUserName = false;
                    g_isInputUserID = false;
                    g_isInputPassword = false;
                    g_isInputQuestion = false;
                    g_isInputAnswer = false;
                    g_cursorPos = g_confirmPassword.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnQuestion.isHover) {
                    g_isInputQuestion = true;
                    g_isInputUserName = false;
                    g_isInputUserID = false;
                    g_isInputPassword = false;
                    g_isInputConfirmPassword = false;
                    g_isInputAnswer = false;
                    g_cursorPos = g_question.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnAnswer.isHover) {
                    g_isInputAnswer = true;
                    g_isInputUserName = false;
                    g_isInputUserID = false;
                    g_isInputPassword = false;
                    g_isInputConfirmPassword = false;
                    g_isInputQuestion = false;
                    g_cursorPos = g_answer.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnEsc2.isHover) {
                    signIn();
                }
                else {
                    g_isInputUserName = false;
                    g_isInputUserID = false;
                    g_isInputPassword = false;
                    g_isInputConfirmPassword = false;
                    g_isInputQuestion = false;
                    g_isInputAnswer = false;
                }

                // 廣過梓泥泣似
                if (g_btnRegister2.isHover) {
                    // 刮屬補秘
                    if (g_userName.empty() || g_userId.empty() || g_password.empty() ||
                        g_confirmPassword.empty() || g_question.empty() || g_answer.empty()) {
                        MessageBox(NULL, _T("请填写所有字段"), _T("错误"), MB_OK);

                        // 激活第一个为空的输入框
                        if (g_userName.empty()) {
                            g_isInputUserName = true;
                            g_cursorPos = 0;
                        }
                        else if (g_userId.empty()) {
                            g_isInputUserID = true;
                            g_cursorPos = 0;
                        }
                        else if (g_password.empty()) {
                            g_isInputPassword = true;
                            g_cursorPos = 0;
                        }
                        else if (g_confirmPassword.empty()) {
                            g_isInputConfirmPassword = true;
                            g_cursorPos = 0;
                        }
                        else if (g_question.empty()) {
                            g_isInputQuestion = true;
                            g_cursorPos = 0;
                        }
                        else if (g_answer.empty()) {
                            g_isInputAnswer = true;
                            g_cursorPos = 0;
                        }
                        g_showCursor = true;
                        g_cursorTimer = clock();
                    }
                    else if (g_password != g_confirmPassword) {
                        MessageBox(NULL, _T("两次输入的密码不一致"), _T("错误"), MB_OK);
                        // 激活密码输入框
                        g_isInputPassword = true;
                        g_isInputConfirmPassword = true;
                        g_cursorPos = g_password.length();
                        g_showCursor = true;
                        g_cursorTimer = clock();
                    }
                    else {
                        // 距喘廣過痕方
                        if (registerUser(g_userId, g_userName, g_password, g_question, g_answer)) {
                            MessageBox(NULL, _T("注册成功"), _T("提示"), MB_OK);
                            return; // 卦指鞠村順中
                        }
                    }
                }
                break;
            }
            case WM_KEYDOWN:
                if (msg.vkcode == VK_BACK) {
                    if (g_isInputUserName && g_cursorPos > 0) {
                        g_userName.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputUserID && g_cursorPos > 0) {
                        g_userId.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputPassword && g_cursorPos > 0) {
                        g_password.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputConfirmPassword && g_cursorPos > 0) {
                        g_confirmPassword.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputQuestion && g_cursorPos > 0) {
                        g_question.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputAnswer && g_cursorPos > 0) {
                        g_answer.erase(--g_cursorPos, 1);
                    }
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_LEFT) {
                    g_cursorPos = max(0, g_cursorPos - 1);
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_RIGHT) {
                    int maxPos = 0;
                    if (g_isInputUserName) maxPos = g_userName.length();
                    else if (g_isInputUserID) maxPos = g_userId.length();
                    else if (g_isInputPassword) maxPos = g_password.length();
                    else if (g_isInputConfirmPassword) maxPos = g_confirmPassword.length();
                    else if (g_isInputQuestion) maxPos = g_question.length();
                    else if (g_isInputAnswer) maxPos = g_answer.length();

                    g_cursorPos = min(maxPos, g_cursorPos + 1);
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_TAB) {
                    if (g_isInputUserName) {
                        // 用户名框回车 -> 切换到ID框
                        g_isInputUserName = false;
                        g_isInputUserID = true;
                        g_cursorPos = g_userId.length();
                    }
                    else if (g_isInputUserID) {
                        // ID框回车 -> 切换到密码框
                        g_isInputUserID = false;
                        g_isInputPassword = true;
                        g_cursorPos = g_password.length();
                    }
                    else if (g_isInputPassword) {
                        // 密码框回车 -> 切换到确认密码框
                        g_isInputPassword = false;
                        g_isInputConfirmPassword = true;
                        g_cursorPos = g_confirmPassword.length();
                    }
                    else if (g_isInputConfirmPassword) {
                        // 确认密码框回车 -> 切换到问题框
                        g_isInputConfirmPassword = false;
                        g_isInputQuestion = true;
                        g_cursorPos = g_question.length();
                    }
                    else if (g_isInputQuestion) {
                        // 问题框回车 -> 切换到答案框
                        g_isInputQuestion = false;
                        g_isInputAnswer = true;
                        g_cursorPos = g_answer.length();
                    }
                    else if (g_isInputAnswer) {
                        g_isInputUserName = true; 
                        g_isInputAnswer = false;
                        g_cursorPos = g_userName.length();
                    }

                    else {
                        g_isInputUserName = true;
                        g_isInputUserID = false;
                        g_isInputPassword = false;
                        g_isInputConfirmPassword = false;
                        g_isInputQuestion = false;
                        g_isInputAnswer = false;
                        g_cursorPos = g_userName.length();
                    }
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_RETURN) {
                    if (g_isInputUserName) {
                        // 用户名框回车 -> 切换到ID框
                        g_isInputUserName = false;
                        g_isInputUserID = true;
                        g_cursorPos = g_userId.length();
                    }
                    else if (g_isInputUserID) {
                        // ID框回车 -> 切换到密码框
                        g_isInputUserID = false;
                        g_isInputPassword = true;
                        g_cursorPos = g_password.length();
                    }
                    else if (g_isInputPassword) {
                        // 密码框回车 -> 切换到确认密码框
                        g_isInputPassword = false;
                        g_isInputConfirmPassword = true;
                        g_cursorPos = g_confirmPassword.length();
                    }
                    else if (g_isInputConfirmPassword) {
                        // 确认密码框回车 -> 切换到问题框
                        g_isInputConfirmPassword = false;
                        g_isInputQuestion = true;
                        g_cursorPos = g_question.length();
                    }
                    else if (g_isInputQuestion) {
                        // 问题框回车 -> 切换到答案框
                        g_isInputQuestion = false;
                        g_isInputAnswer = true;
                        g_cursorPos = g_answer.length();
                    }
                    else if (g_isInputAnswer) {
                        // 答案框回车 -> 触发注册操作
                        // 验证输入
                        if (g_userName.empty() || g_userId.empty() || g_password.empty() ||
                            g_confirmPassword.empty() || g_question.empty() || g_answer.empty())
                        {
                            MessageBox(NULL, _T("请填写所有字段"), _T("错误"), MB_OK);

                            // 激活第一个为空的输入框
                            if (g_userName.empty()) {
                                g_isInputUserName = true;
                                g_cursorPos = 0;
                            }
                            else if (g_userId.empty()) {
                                g_isInputUserID = true;
                                g_cursorPos = 0;
                            }
                            else if (g_password.empty()) {
                                g_isInputPassword = true;
                                g_cursorPos = 0;
                            }
                            else if (g_confirmPassword.empty()) {
                                g_isInputConfirmPassword = true;
                                g_cursorPos = 0;
                            }
                            else if (g_question.empty()) {
                                g_isInputQuestion = true;
                                g_cursorPos = 0;
                            }
                            else if (g_answer.empty()) {
                                g_isInputAnswer = true;
                                g_cursorPos = 0;
                            }
                        }
                        else if (g_password != g_confirmPassword) {
                            MessageBox(NULL, _T("两次输入的密码不一致"), _T("错误"), MB_OK);
                            // 激活密码输入框
                            g_isInputPassword = true;
                            g_isInputConfirmPassword = true;
                            g_cursorPos = g_password.length();
                        }
                        else {
                            // 调用注册函数
                            if (registerUser(g_userId, g_userName, g_password, g_question, g_answer)) {
                                MessageBox(NULL, _T("注册成功"), _T("提示"), MB_OK);
                                return; // 返回登录界面
                            }
                            else {
                                MessageBox(NULL, _T("注册失败，用户ID可能已存在"), _T("错误"), MB_OK);
                            }
                        }
                    }

                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_DOWN) {
                    if (g_isInputUserName) {
                        g_isInputUserName = false;
                        g_isInputUserID = true;
                        g_cursorPos = g_userId.length();
                    }
                    else if (g_isInputUserID) {
                        g_isInputUserID = false;
                        g_isInputPassword = true;
                        g_cursorPos = g_password.length();
                    }
                    else if (g_isInputPassword) {
                        g_isInputPassword = false;
                        g_isInputConfirmPassword = true;
                        g_cursorPos = g_confirmPassword.length();
                    }
                    else if (g_isInputConfirmPassword) {
                        g_isInputConfirmPassword = false;
                        g_isInputQuestion = true;
                        g_cursorPos = g_question.length();
                    }
                    else if (g_isInputQuestion) {
                        g_isInputQuestion = false;
                        g_isInputAnswer = true;
                        g_cursorPos = g_answer.length();
                    }
                    else if (g_isInputAnswer) {
                        g_isInputAnswer = false;
                        g_isInputUserName = true;
                        g_cursorPos = g_userName.length();
                    }
                    g_showCursor = true;
                    g_cursorTimer = clock();
                    }
                else if (msg.vkcode == VK_UP) {
                        if (g_isInputUserName) {
                            g_isInputUserName = false;
                            g_isInputAnswer = true;
                            g_cursorPos = g_answer.length();
                        }
                        else if (g_isInputUserID) {
                            g_isInputUserID = false;
                            g_isInputUserName = true;
                            g_cursorPos = g_userName.length();
                        }
                        else if (g_isInputPassword) {
                            g_isInputPassword = false;
                            g_isInputUserID = true;
                            g_cursorPos = g_userId.length();
                        }
                        else if (g_isInputConfirmPassword) {
                            g_isInputConfirmPassword = false;
                            g_isInputPassword = true;
                            g_cursorPos = g_password.length();
                        }
                        else if (g_isInputQuestion) {
                            g_isInputQuestion = false;
                            g_isInputConfirmPassword = true;
                            g_cursorPos = g_confirmPassword.length();
                        }
                        else if (g_isInputAnswer) {
                            g_isInputAnswer = false;
                            g_isInputQuestion = true;
                            g_cursorPos = g_question.length();
                        }
                        g_showCursor = true;
                        g_cursorTimer = clock();
                        }
                break;
            
            case WM_CHAR:
                handleRegisterInput(msg.message, msg.wParam, msg.lParam);
                break;
            }
        }

        // 侃尖高炎描妨
        if (clock() - g_cursorTimer > 500) {
            g_showCursor = !g_showCursor;
            g_cursorTimer = clock();
        }

        // 紙崙順中
        registerUI();

        // 耶紗玉壙决岳
        Sleep(10);
    }
}

// 注册UI
void registerUI() {
    BeginBatchDraw();

    loadimage(&g_bgImg1, _T("png\\g_bgImg1.png"));
    putimage(0, 0, &g_bgImg1);

    // 紙崙侭嗤猟云崇
    drawTextBox(g_btnUserName);
    drawTextBox(g_btnUserID2);
    drawTextBox(g_btnPassword2);
    drawTextBox(g_btnConfirmPassword);
    drawTextBox(g_btnQuestion);
    drawTextBox(g_btnAnswer);
    drawTextBox(g_btnRegister2);
    drawTextBox(g_btnEsc2);

    settextstyle(24, 0, _T("宋体"));
    setbkmode(TRANSPARENT);
    settextcolor(WHITE);
    outtextxy(45, 150, _T("用户名"));
    outtextxy(45, 200, _T("ID"));
    outtextxy(45, 250, _T("密码"));
    outtextxy(45, 300, _T("确认密码"));
    outtextxy(45, 350, _T("密保问题"));
    outtextxy(45, 400, _T("答案"));

    settextcolor(BLACK);
    settextstyle(22, 0, _T("宋体"));
    outtextxy(183, 463, _T("注册"));

    settextcolor(RED);
    settextstyle(24, 0, _T("宋体"));
    outtextxy(322, 652, _T("退出"));

    // 显示提示文本（占位符）
    if (g_userName.empty() && !g_isInputUserName) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnUserName.x + 5, g_btnUserName.y + 5, _T("请输入用户名"));
    }
    if (g_userId.empty() && !g_isInputUserID) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnUserID2.x + 5, g_btnUserID2.y + 5, _T("请输入ID"));
    }
    if (g_password.empty() && !g_isInputPassword) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnPassword2.x + 5, g_btnPassword2.y + 5, _T("请输入密码"));
    }
    if (g_confirmPassword.empty() && !g_isInputConfirmPassword) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnConfirmPassword.x + 5, g_btnConfirmPassword.y + 5, _T("确认密码"));
    }
    if (g_question.empty() && !g_isInputQuestion) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnQuestion.x + 5, g_btnQuestion.y + 5, _T("请输入密保问题"));
    }
    if (g_answer.empty() && !g_isInputAnswer) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnAnswer.x + 5, g_btnAnswer.y + 5, _T("请输入答案"));
    }

    // 𠮟幣補秘坪否
    settextcolor(BLACK);
    if (!g_userName.empty() || g_isInputUserName) {
        outtextxy(g_btnUserName.x + 5, g_btnUserName.y + 5, g_userName.c_str());
    }
    if (!g_userId.empty() || g_isInputUserID) {
        outtextxy(g_btnUserID2.x + 5, g_btnUserID2.y + 5, g_userId.c_str());
    }

    // 畜鷹𠮟幣葎*
    if (!g_password.empty() || g_isInputPassword) {
        std::wstring showPass(g_password.size(), PASSWORD_MASK);
        outtextxy(g_btnPassword2.x + 5, g_btnPassword2.y + 5, showPass.c_str());
    }
    if (!g_confirmPassword.empty() || g_isInputConfirmPassword) {
        std::wstring showConfirmPass(g_confirmPassword.size(), PASSWORD_MASK);
        outtextxy(g_btnConfirmPassword.x + 5, g_btnConfirmPassword.y + 5, showConfirmPass.c_str());
    }
    if (!g_question.empty() || g_isInputQuestion) {
        outtextxy(g_btnQuestion.x + 5, g_btnQuestion.y + 5, g_question.c_str());
    }
    if (!g_answer.empty() || g_isInputAnswer) {
        outtextxy(g_btnAnswer.x + 5, g_btnAnswer.y + 5, g_answer.c_str());
    }

    // 高炎侃尖
    if (clock() - g_cursorTimer > 500) {
        g_showCursor = !g_showCursor;
        g_cursorTimer = clock();
    }

    if (g_showCursor) {
        Button* activeBtn = nullptr;
        std::wstring* currentStr = nullptr;
        if (g_isInputUserName) {
            activeBtn = &g_btnUserName;
            currentStr = &g_userName;
        }
        else if (g_isInputUserID) {
            activeBtn = &g_btnUserID2;
            currentStr = &g_userId;
        }
        else if (g_isInputPassword) {
            activeBtn = &g_btnPassword2;
            currentStr = &g_password;
        }
        else if (g_isInputConfirmPassword) {
            activeBtn = &g_btnConfirmPassword;
            currentStr = &g_confirmPassword;
        }
        else if (g_isInputQuestion) {
            activeBtn = &g_btnQuestion;
            currentStr = &g_question;
        }
        else if (g_isInputAnswer) {
            activeBtn = &g_btnAnswer;
            currentStr = &g_answer;
        }

        if (activeBtn != nullptr && currentStr != nullptr) {
            std::wstring displayStr = currentStr->substr(0, g_cursorPos);
            int textWidth = textwidth(displayStr.c_str());

            setlinestyle(PS_SOLID, 2);
            setcolor(RGB(0, 150, 255));
            line(activeBtn->x + 5 + textWidth, activeBtn->y + 5,
                activeBtn->x + 5 + textWidth, activeBtn->y + activeBtn->h - 5);
        }
    }

    EndBatchDraw();
}

void loadSecurityQuestion() {
    extern user users[MAX_USER];
    extern int userCount; // 声明全局用户数量

    // 只在用户ID非空时查询
    if (!g_userId.empty()) {
        bool found = false;
        // 遍历用户数据查找匹配项
        for (int i = 0; i < userCount; ++i) {
            if (users[i].username == g_userId) {
                g_question = users[i].question; // 加载保密问题
                found = true;
                break; // 找到后立即退出循环
            }
        }
        if (!found) {
            g_question.clear(); // 未找到用户时清空问题
        }
    }
}

// 梨芝畜鷹順中
void forgetPassword() {

    static bool lastInputUserID = g_isInputUserID;

    ExMessage msg;
    extern const COLORREF ACTIVE_BORDER_COLOR;
    extern user users[MAX_USER];
    users[0].loadDataFromFile(L".\\data\\ManManage.txt");

    // 嶷崔補秘
    g_userId.clear();
    g_newPassword.clear();
    g_question.clear();
    g_answer.clear();
    g_isInputUserID = false;
    g_isInputNewPassword = false;
    g_isInputQuestion = false;
    g_isInputAnswer = false;

    while (true) {
       
        while (peekmessage(&msg, -1)) {
            if (msg.message == WM_QUIT) {
                return;
            }

            switch (msg.message) {
            case WM_MOUSEMOVE: {
                updateButtonState(g_btnUserID3, msg);
                updateButtonState(g_btnNewPassword, msg);
                updateButtonState(g_btnQuestion3, msg);
                updateButtonState(g_btnAnswer3, msg);
                updateButtonState(g_btnResetPassword, msg);
                updateButtonState(g_btnEsc3, msg);
                break;
            }
            case WM_LBUTTONDOWN: {
                updateButtonState(g_btnUserID3, msg);
                updateButtonState(g_btnNewPassword, msg);
                updateButtonState(g_btnQuestion3, msg);
                updateButtonState(g_btnAnswer3, msg);
                updateButtonState(g_btnResetPassword, msg);
                updateButtonState(g_btnEsc3, msg);

                // 侃尖補秘崇泣似
                if (g_btnUserID3.isHover) {
                    g_isInputUserID = true;
                    g_isInputNewPassword = false;
                    g_isInputQuestion = false;
                    g_isInputAnswer = false;
                    g_cursorPos = g_userId.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnNewPassword.isHover) {
                    g_isInputNewPassword = true;
                    g_isInputUserID = false;
                    g_isInputQuestion = false;
                    g_isInputAnswer = false;
                    g_cursorPos = g_newPassword.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnQuestion3.isHover) {
                    g_isInputQuestion = true;
                    g_isInputUserID = false;
                    g_isInputNewPassword = false;
                    g_isInputAnswer = false;
                    g_cursorPos = g_question.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnAnswer3.isHover) {
                    g_isInputAnswer = true;
                    g_isInputUserID = false;
                    g_isInputNewPassword = false;
                    g_isInputQuestion = false;
                    g_cursorPos = g_answer.length();
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (g_btnEsc3.isHover) {
                    signIn();
                }
                else {
                    g_isInputUserID = false;
                    g_isInputNewPassword = false;
                    g_isInputQuestion = false;
                    g_isInputAnswer = false;
                }

                // 嶷崔畜鷹梓泥泣似
                if (g_btnResetPassword.isHover) {
                    // 刮屬補秘
                    if (g_userId.empty() || g_newPassword.empty() ||
                        g_question.empty() || g_answer.empty()) {
                        MessageBox(NULL, _T("请填写所有字段"), _T("错误"), MB_OK);
                    }
                    else {
                        // 距喘畜鷹嶷崔痕方
                        if (resetPassword(g_userId, g_newPassword, g_question, g_answer)) {
                            MessageBox(NULL, _T("密码重置成功"), _T("提示"), MB_OK);
                            return; // 卦指鞠村順中
                        }
                    }
                }
                break;
            }
            case WM_KEYDOWN:
                if (msg.vkcode == VK_BACK) {
                    if (g_isInputUserID && g_cursorPos > 0) {
                        g_userId.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputNewPassword&& g_cursorPos > 0) {
                        g_newPassword.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputQuestion && g_cursorPos > 0) {
                        g_question.erase(--g_cursorPos, 1);
                    }
                    else if (g_isInputAnswer && g_cursorPos > 0) {
                        g_answer.erase(--g_cursorPos, 1);
                    }
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_LEFT) {
                    g_cursorPos = max(0, g_cursorPos - 1);
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_RIGHT) {
                    int maxPos = 0;
                    if (g_isInputUserID) maxPos = g_userId.length();
                    else if (g_isInputNewPassword) maxPos = g_newPassword.length();
                    else if (g_isInputQuestion) maxPos = g_question.length();
                    else if (g_isInputAnswer) maxPos = g_answer.length();

                    g_cursorPos = min(maxPos, g_cursorPos + 1);
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
                else if (msg.vkcode == VK_TAB) {
                    if (g_isInputUserID) {
                        // ID框回车 -> 切换到密码框
                        g_isInputUserID = false;
                        g_isInputNewPassword = true;
                        g_cursorPos = g_password.length();
                    }
                    else if (g_isInputNewPassword) {
                        // 密码框回车 -> 切换到确认密码框
                        g_isInputNewPassword = false;
                        g_isInputQuestion = true;
                        g_cursorPos = g_confirmPassword.length();
                    }
                    else if (g_isInputQuestion) {
                        // 问题框回车 -> 切换到答案框
                        g_isInputQuestion = false;
                        g_isInputAnswer = true;
                        g_cursorPos = g_answer.length();
                    }
                    else if (g_isInputAnswer) {
                        g_isInputUserID = true;
                        g_isInputAnswer = false;
                        g_cursorPos = g_userName.length();
                    }

                    else {
                        g_isInputUserID = true;
                        g_isInputNewPassword = false;
                        g_isInputQuestion = false;
                        g_isInputAnswer = false;
                        g_cursorPos = g_userName.length();
                    }
                    g_showCursor = true;
                    g_cursorTimer = clock();
                }
				else if (msg.vkcode == VK_RETURN) {
					if (g_isInputUserID) {
						// ID框回车 -> 切换到密码框
						g_isInputUserID = false;
						g_isInputNewPassword = true;
						g_cursorPos = g_newPassword.length();
					}
					else if (g_isInputNewPassword) {
						// 密码框回车 -> 切换到确认密码框
						g_isInputNewPassword = false;
						g_isInputQuestion = true;
						g_cursorPos = g_question.length();
					}
					else if (g_isInputQuestion) {
						// 问题框回车 -> 切换到答案框
						g_isInputQuestion = false;
						g_isInputAnswer = true;
						g_cursorPos = g_answer.length();
					}
					else if (g_isInputAnswer) {
						// 答案框回车 -> 触发重置密码操作
						// 验证输入
						if (g_userId.empty() || g_newPassword.empty() ||
							g_question.empty() || g_answer.empty())
						{
							MessageBox(NULL, _T("请填写所有字段"), _T("错误"), MB_OK);
						}
						else {
							// 调用重置密码函数
							if (resetPassword(g_userId, g_newPassword, g_question, g_answer)) {
								MessageBox(NULL, _T("密码重置成功"), _T("提示"), MB_OK);
								return; // 返回登录界面
							}
							else {
								MessageBox(NULL, _T("重置密码失败，可能是用户ID不存在或问题答案不正确"), _T("错误"), MB_OK);
							}
						}
					}
					g_showCursor = true;
					g_cursorTimer = clock();
				}
                else if (msg.vkcode == VK_DOWN) {
                    if (g_isInputUserID) {
                        g_isInputUserID = false;
                        g_isInputNewPassword = true;
                        g_cursorPos = g_newPassword.length();
                    }
                    else if (g_isInputNewPassword) {
                        g_isInputNewPassword = false;
                        g_isInputQuestion = true;
                        g_cursorPos = g_question.length();
                    }
                    else if (g_isInputQuestion) {
                        g_isInputQuestion = false;
                        g_isInputAnswer = true;
                        g_cursorPos = g_answer.length();
                    }
                    else if (g_isInputAnswer) {
                        g_isInputAnswer = false;
                        g_isInputUserID = true;
                        g_cursorPos = g_userId.length();
                    }
                    g_showCursor = true;
                    g_cursorTimer = clock();
                    }
                else if (msg.vkcode == VK_UP) {
                        if (g_isInputUserID) {
                            g_isInputUserID = false;
                            g_isInputAnswer = true;
                            g_cursorPos = g_answer.length();
                        }
                        else if (g_isInputNewPassword) {
                            g_isInputNewPassword = false;
                            g_isInputUserID = true;
                            g_cursorPos = g_userId.length();
                        }
                        else if (g_isInputQuestion) {
                            g_isInputQuestion = false;
                            g_isInputNewPassword = true;
                            g_cursorPos = g_newPassword.length();
                        }
                        else if (g_isInputAnswer) {
                            g_isInputAnswer = false;
                            g_isInputQuestion = true;
                            g_cursorPos = g_question.length();
                        }
                        g_showCursor = true;
                        g_cursorTimer = clock();
                        }
                break;
            case WM_CHAR:
                handleForgetInput(msg.message, msg.wParam, msg.lParam);
                break;
            }
        }

        if (lastInputUserID && !g_isInputUserID) {
            loadSecurityQuestion(); // ID框失去焦点时加载问题
            g_answer.clear();       // 清空答案（因为问题已变）
        }
        lastInputUserID = g_isInputUserID; // 更新状态

        // 侃尖高炎描妨
        if (clock() - g_cursorTimer > 500) {
            g_showCursor = !g_showCursor;
            g_cursorTimer = clock();
        }

        // 紙崙順中
        forgetUI();

        // 耶紗玉壙决岳
        Sleep(10);
    }
}

// 梨芝畜鷹順中紙崙
void forgetUI() {
    BeginBatchDraw();

    loadimage(&g_bgImg1, _T("png\\g_bgImg1.png"));
    putimage(0, 0, &g_bgImg1);

    // 紙崙侭嗤猟云崇
    drawTextBox(g_btnUserID3);
    drawTextBox(g_btnNewPassword);
    drawTextBox(g_btnQuestion3);
    drawTextBox(g_btnAnswer3);
    drawTextBox(g_btnResetPassword);
    drawTextBox(g_btnEsc3);

    settextstyle(24, 0, _T("宋体"));
    setbkmode(TRANSPARENT);
    settextcolor(WHITE);
    outtextxy(45, 200, _T("ID"));
    outtextxy(45, 250, _T("新密码"));
    outtextxy(45, 300, _T("密保问题"));
    outtextxy(45, 350, _T("答案"));

    settextcolor(BLACK);
    settextstyle(22, 0, _T("宋体"));
    outtextxy(183, 413, _T("重置密码"));

    settextcolor(RED);
    settextstyle(24, 0, _T("宋体"));
    outtextxy(322, 652, _T("退出"));

    // 显示提示文本（占位符）
    if (g_userId.empty() && !g_isInputUserID) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnUserID3.x + 5, g_btnUserID3.y + 5, _T("请输入ID"));
    }
    if (g_newPassword.empty() && !g_isInputNewPassword) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnNewPassword.x + 5, g_btnNewPassword.y + 5, _T("请输入新密码"));
    }
    if (g_question.empty() && !g_isInputQuestion) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnQuestion3.x + 5, g_btnQuestion3.y + 5, _T("请输入保密问题"));
    }
    if (g_answer.empty() && !g_isInputAnswer) {
        settextcolor(RGB(150, 150, 150));
        outtextxy(g_btnAnswer3.x + 5, g_btnAnswer3.y + 5, _T("请输入答案"));
    }

    // 𠮟幣補秘坪否
    settextcolor(BLACK);
    if (!g_userId.empty() || g_isInputUserID) {
        outtextxy(g_btnUserID3.x + 5, g_btnUserID3.y + 5, g_userId.c_str());
    }

    // 畜鷹𠮟幣葎*
    if (!g_newPassword.empty() || g_isInputNewPassword) {
        std::wstring showNewPass(g_newPassword.size(), PASSWORD_MASK);
        outtextxy(g_btnNewPassword.x + 5, g_btnNewPassword.y + 5, showNewPass.c_str());
    }

    if (!g_question.empty() || g_isInputQuestion) {
        outtextxy(g_btnQuestion3.x + 5, g_btnQuestion3.y + 5, g_question.c_str());
    }

    if (!g_answer.empty() || g_isInputAnswer) {
        outtextxy(g_btnAnswer3.x + 5, g_btnAnswer3.y + 5, g_answer.c_str());
    }

    // 高炎侃尖
    if (clock() - g_cursorTimer > 500) {
        g_showCursor = !g_showCursor;
        g_cursorTimer = clock();
    }

    if (g_showCursor) {
        Button* activeBtn = nullptr;
        std::wstring* currentStr = nullptr;
        if (g_isInputUserID) {
            activeBtn = &g_btnUserID3;
            currentStr = &g_userId;
        }
        else if (g_isInputNewPassword) {
            activeBtn = &g_btnNewPassword;
            currentStr = &g_newPassword;
        }
        else if (g_isInputQuestion) {
            activeBtn = &g_btnQuestion3;
            currentStr = &g_question;
        }
        else if (g_isInputAnswer) {
            activeBtn = &g_btnAnswer3;
            currentStr = &g_answer;
        }

        if (activeBtn != nullptr && currentStr != nullptr) {
            std::wstring displayStr = currentStr->substr(0, g_cursorPos);
            int textWidth = textwidth(displayStr.c_str());

            setlinestyle(PS_SOLID, 2);
            setcolor(RGB(0, 150, 255));
            line(activeBtn->x + 5 + textWidth, activeBtn->y + 5,
                activeBtn->x + 5 + textWidth, activeBtn->y + activeBtn->h - 5);
        }
    }

    EndBatchDraw();
}